require 'rubygems'
require 'mytree'
require 'Parser'
require 'supporting_functions'
require 'cleanup_js'
require 'type_tracker'

filename = "/Users/apridgen/index.php"

mytree = MyTree.new()
mytree.parse_file(filename)
nodes = mytree.find_all_node_tag("script")

new_codes = []


#redbug stuff
# eval $my_const = {}
# eval $consts.each {|k,v| $my_const[v] = k}
# e pop = get_children(n.body[0][9].initializer)

$my_const = {}
$consts.each {|k,v| $my_const[v] = k}

nodes.each do |node|
  code = node.data
  #new_code = cleanup_js(code)
  
  #v = build_var_table(new_code)
  #print "Before clean-up js\n\n#{code}\n\n"      
  new_codes << code
end

nc = new_codes.join("\n")
#print "#{nc}\n"
#print nc
jstree = parse(nc, "", 0)
#test_tree = parse('String["fromCharToCode"](x);',"",0)
#puts 'finished parsing'
#print "#{jstree}"
#print_assignments(jstree)
#functions = get_functions(jstree)

#functions.each_key {|name| print "#{name}\n"}

children = get_children(jstree)

n = children[0]

attrs = [n.value,
    n.lineno, n.start, n.end,
    n.name, n.params, n.funDecls, n.varDecls, n.body, n.functionForm,
    n.assignOp, n.expression, n.condition, n.thenPart, n.elsePart]

j = children[1]

get_functions(j)
f = parse_expression(j)